<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Search engine for blogmer.
  Author: Yashpal
  		

  Function name : 
  search   -> Give search result for given keyword and area in database.
 

*/


class Search_model extends Model 
{
    function Search_model()
    { parent::Model();}
	
	function search($place,$keyword)
	{
		
        if($keyword != '')
		{
			
			
			
			switch($place)
			{
				case 'post':	
								$this->db->like('Content', $keyword);
								$this->db->from('posts');								
								break;
				
				case 'page':	$this->db->like('Content', $keyword);
								$this->db->from('pages');
								break;
								
				case 'tag':		$this->db->like('TagClouds', $keyword);
								$this->db->from('posts');
								break;				

				default:		return "NO such area found";
								
			}
			
			$cnt=$this->db->count_all_results();
			
			if($cnt==0)
			{	
			
				$data['title']="Information Page";
				$data['header']="Information";
				$data['url']="/blog/";
				$data['time']="4";
				$data['message']="<h3>Your search did not return any results.<h3><p><a href='JavaScript:history.go(-1)'>Return to Previous Page</a></p><p>Or wait for auto redirection</p>
";
				$this->load->view('info',$data);
			}

			$config['base_url'] = site_url().'blog/search/'.$place.'/'.$keyword;
			$config['total_rows'] =$cnt;
			$config['per_page'] = '10';
			$config['num_links'] = '3';
			$config['uri_segment'] = '5';
			
			$this->pagination->initialize($config); 
			$strr='<h3>Search Result for : <em>'.$keyword.'</em></h3>';
			//$this->db->order_by("PostID", "desc"); 		
			switch($place)
			{
				case 'post':	$this->db->order_by("PostID", "desc");
								$this->db->like('Content', $keyword);
								$this->db->or_like('Title', $keyword);
								$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(5));								
								
								foreach ($query->result() as $row)
								{
									//echo  $row->Title."    By : ".$row->Author."<br>";
									$strr.= "<p>".anchor('blog/post/'.$row->URLTitle,$row->Title)."</p>";
									
								}
								break;

				
				case 'page':	$this->db->order_by("PageID", "desc");
								$this->db->like('Content', $keyword);
								$query = $this->db->get('pages',$config['per_page'],$this->uri->segment(5));

								
								foreach ($query->result() as $row)
								{
									//echo  $row->Title."    By : ".$row->Author."<br>";
									$strr.= "<p>".anchor('blog/page/'.$row->URLTitle,$row->Title)."</p>";
									
								}
								break;
								
				case 'tag':		$this->db->order_by("PostID", "desc");
								$this->db->like('TagClouds', $keyword);
								$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(5));
								foreach ($query->result() as $row)
								{
									//echo  $row->Title."    By : ".$row->Author."<br>";
									$strr.= "<p>".anchor('blog/post/'.$row->URLTitle,$row->Title)."</p>";
									
								}
								break;				

				default:		return "N0 such area found";
								
			}
	
	      
		
		$strr.= '<span class="nav-link">'.$this->pagination->create_links().'</span>';
		return $strr;
		
	    }
		else
		{
			return "There are no keywords to search";
		}
		
		return "Unexpected!!";

	} //end of function 
	
} // end of class


			
?>